
/*
 * Name: injectdll
 * Version: 0.1.0
 * Description: A simple program to inject a DLL in a running process
 * Dependencies: bzb
 */

#include "bzbio.h"
#include "bzbfile.h"
#include "bzbmem.h"
#include "bzbutil.h"

constexpr int VERSION_MAJOR = 0;
constexpr int VERSION_MINOR = 1;
constexpr int VERSION_PATCH = 0;

using namespace bzbio;

int main(int argc, char const *argv[]) {

	std::string exename;
	std::string dllname;

	println("Executable name:");
	getline(std::cin, exename);

	println("DLL name:");
	getline(std::cin, dllname);

	auto pids = bzbmem::get_pid_by_name(exename);

	if(pids.size() == 0) {
		println("No process found...");
		exit(0);
	}

	println("Processes created by ", exename);
	for(size_t i = 0; i < pids.size(); ++i) {
		println('[', i, ']', "\t", pids[i]);
	}

	size_t selection;
	print("\n> ");
	std::cin >> selection;

	if(selection > pids.size()) {
		println("ERROR Incorrect value");
		exit(1);
	}

	int pid = pids[selection];
	unsigned long int ret;

	println("Injecting DLL into process with PID ", pid, "...");
	try {
		ret = bzbmem::inject_dll(pid, dllname);
	} catch (std::system_error err) {
		println("ERROR ", err.what());
		exit(1);
	}

	println("Process PID: ", ret);

	return 0;
}
